package com.taiyuan.tongcheng.sample;

import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.KJHttp;
import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.http.HttpConfig;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.utils.StringUtils;

import android.annotation.SuppressLint;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.taiyuan.tongcheng.http.TcWebservice;
import com.taiyuan.tongcheng.widget.EmptyLayout;
import com.taiyuan.tongcheng.widget.listview.FooterLoadingLayout;
import com.taiyuan.tongcheng.widget.listview.PullToRefreshBase;
import com.taiyuan.tongcheng.widget.listview.PullToRefreshList;
import com.taiyuan.tongcheng.widget.listview.PullToRefreshBase.OnRefreshListener;

public class PullListSample extends KJActivity{

//    @BindView(绑定空数据显示控件id)
    private EmptyLayout mEmptyLayout;
//    @BindView(上拉刷控件id)
    private PullToRefreshList mRefreshLayout;
    private ListView mList;
    
    //http工具
    private KJHttp  kjHttp;
    private final String GET_CARGO_DATA = TcWebservice.WEBSERVER_IP+TcWebservice.API.FREIGHT_ORDER_GET_LIST;
    private String orderListcache; //订单缓存集合
    private HttpParams  httpParame;
    
	
	
	@Override
	public void setRootView() {
		
	}
    
	
	
	@Override
	public void initWidget() {
		// TODO Auto-generated method stub
		super.initWidget();
		// 点击重新加载
		mEmptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
				refresh();
			}
		});
		listViewPreference();
		fillOrderData();
	}
	@Override
	public void initData() {
		super.initData();
		
		// 初始化话http配置
		HttpConfig config = new HttpConfig();
		int hour = StringUtils.toInt(StringUtils.getDataTime("HH"), 0);
		if (hour > 12 && hour < 22) {
			config.cacheTime = 10;
		} else {
			config.cacheTime = 300;
		}
		config.useDelayCache = true;
		kjHttp = new KJHttp(config);
		
	}
	
	
	 /**
	  * 
	 * @Description: 初始话样式
	 * @return void
	 * @throws
	 * @autour Administrator
	 * @date 2015-8-30 下午12:37:39 
	 * @update (date)
	  */
   @SuppressLint("NewApi")
	private void listViewPreference() {
       mList = mRefreshLayout.getRefreshView();
       mList.setDivider(new ColorDrawable(android.R.color.transparent));
       mList.setOverscrollFooter(null);
       mList.setOverscrollHeader(null);
       mList.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
       mRefreshLayout.setPullLoadEnabled(true);
       ((FooterLoadingLayout) mRefreshLayout.getFooterLoadingLayout())
               .setNoMoreDataText("学习不可贪多哦~");

       mList.setOnItemClickListener(new OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view,
                   int position, long id) {
           	
           	//列表点击方法
           }
       });

       mRefreshLayout.setOnRefreshListener(new OnRefreshListener<ListView>() {
           @Override
           public void onPullDownToRefresh(
                   PullToRefreshBase<ListView> refreshView) {
               refresh();
           }

           @Override
           public void onPullUpToRefresh(
                   PullToRefreshBase<ListView> refreshView) {
               mRefreshLayout.setHasMoreData(false);
           }
       });
   }
	
	
   private void fillOrderData() {
   	orderListcache = kjHttp.getStringCache(GET_CARGO_DATA);
       if (!StringUtils.isEmpty(orderListcache)) {
           
           //关闭空数据显示
           mEmptyLayout.dismiss();
       }
       refresh();
   }

   
   private void refresh() {
   	
   	httpParame =new HttpParams();
   	httpParame.put("XXX", "xxxx");//数据
   	
   	kjHttp.post(GET_CARGO_DATA, httpParame, new HttpCallBack() {
   		
   		@Override
   		public void onSuccess(String t) {
   			super.onSuccess(t);
   			//获取成功,进行逻辑 
   		}
   		
   		
   		@Override
   		public void onFailure(int errorNo, String strMsg) {
   			super.onFailure(errorNo, strMsg);
   			//样例数据
   			BaseAdapter  sampAdapter = null ;
   			//获取失败
				if (sampAdapter != null && sampAdapter.getCount() > 0) {
					return;
				} else {
					mEmptyLayout.setErrorType(EmptyLayout.NODATA);
				}
   		}
   		
   		
   		@Override
   		public void onFinish() {
   			// TODO Auto-generated method stub
   			super.onFinish();
   			//关闭上拉、下拉刷写
   	         mRefreshLayout.onPullDownRefreshComplete();
                mRefreshLayout.onPullUpRefreshComplete();
   		}
   		
		});
   	
   	
   }
   
	
	
}
